package common;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class PickImage {

	public static Uri uriFromPath;
	public static final int REQUEST_CODE_PICK_IMAGE = 10;
	public static void pickImageCrop(Activity context) {
		Intent pickImage = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				pickImage.setType("image/*");
				pickImage.putExtra("crop", "true");
				pickImage.putExtra("aspectX", 1);
				pickImage.putExtra("aspectY", 1);
				pickImage.putExtra("outputX", 96);
				pickImage.putExtra("outputY", 96);
				pickImage.putExtra("noFaceDetection", true);
				pickImage.putExtra("return-data", true);
				context.startActivityForResult(pickImage, REQUEST_CODE_PICK_IMAGE);
	}

	public static void pickImage(Activity context) {
		Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        context.startActivityForResult(Intent.createChooser(intent, "Select Picture"), PickImage.REQUEST_CODE_PICK_IMAGE);
	}

	public static Bitmap getPath(Uri uri, Activity context) {
		Bitmap bitmap = null;
		try {
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, o);

			final int REQUIRED_SIZE = 100;

			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp < REQUIRED_SIZE || height_tmp < REQUIRED_SIZE) {
					break;
				}
				width_tmp = 2;
				height_tmp = 2;
				scale *= 2;
			}

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;

			bitmap = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, o2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return bitmap;
	}
	public static Bitmap getPathWithContext(Uri uri, Context context) {
		Bitmap bitmap = null;
		try {
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, o);

			final int REQUIRED_SIZE = 100;

			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp < REQUIRED_SIZE || height_tmp < REQUIRED_SIZE) {
					break;
				}
				width_tmp = 2;
				height_tmp = 2;
				scale *= 2;
			}

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;

			bitmap = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, o2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return bitmap;
	}
}
